package com.wn55.hrms.web.controller;

import com.github.pagehelper.PageInfo;
import com.wn55.hrms.entity.Attendance;
import com.wn55.hrms.entity.Leave;
import com.wn55.hrms.entity.Statistical;
import com.wn55.hrms.service.StatisticalService;
import com.wn55.hrms.service.vo.StatisticalVo;
import com.wn55.hrms.util.ResultResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author Mr.bai~
 * @version 1.0
 * @date 2022/5/6 23:06
 */
@RestController
@RequestMapping("statistical")
@Api(value = "本接口是用来操作统计表记录功能")
public class StatisticalController {

    @Autowired
    StatisticalService statisticalService;


    @GetMapping("/statisticalList")
    @ApiOperation(value = "分页查询渠道列表")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query",dataType = "int",name="now",value = "查询当前页",required = false,defaultValue = "1"),
            @ApiImplicitParam(paramType = "query",dataType = "int",name="size",value ="查询每页条数",required = false,defaultValue = "2"),
            @ApiImplicitParam(paramType = "query",dataType = "string",name="uname",value = "员工名称",required = false)
    })
    public ResultResponse<PageInfo<StatisticalVo>> leaveList(
            @RequestParam(value = "now", defaultValue = "1") Integer now,
            @RequestParam(value = "size", defaultValue = "2") Integer size,
            String uname){
        ResultResponse<PageInfo<StatisticalVo>> result = null;
        try {

            PageInfo<StatisticalVo> pageInfo = statisticalService.queryStatistical(now, size, uname);
            result = new ResultResponse<>(200, "查询成功", pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            result = new ResultResponse<>(404, "查询失败");
        }
        return result;
    }

    @DeleteMapping("/del/{id}")
    @ApiOperation(value = "根据标识ID删除统计")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path",dataType = "int",name = "id",value = "统计的ID标识")
    })
    public ResultResponse<Void> delete(@PathVariable("id") int id) {

        ResultResponse<Void> result = null;

        try {
            statisticalService.removeBykey(id);
            result = new ResultResponse<>(200, "删除统计成功!");
        } catch (Exception e) {
            e.printStackTrace();
            result = new ResultResponse<>(201, "删除统计失败!");
        }
        return result;
    }

    @PostMapping("/add")
    @ApiOperation(value = "添加迟到记录")
    public ResultResponse<Void> add(@RequestBody Statistical statistical) {
        ResultResponse<Void> result = null;
        try {
           statisticalService.add(statistical);
            result = new ResultResponse<>(200, "添加迟到记录成功!");
        } catch (Exception e) {
            e.printStackTrace();
            result = new ResultResponse<>(201, "添加迟到记录失败!");
        }
        return result;
    }

    @PutMapping("/modify")
    @ApiOperation(value = "修改迟到记录")
    public ResultResponse<Void> modifyStatistical(@RequestBody Statistical statistical) {
        ResultResponse<Void> result = null;
        try {
            statisticalService.modifyStatistical(statistical);
            result = new ResultResponse<>(200, "修改迟到记录成功!");
        } catch (Exception e) {
            e.printStackTrace();
            result = new ResultResponse<>(201, "修改迟到记录失败!");
        }
        return result;
    }

}
